/*!
 
 @Name : layPage v1.x - Node.js 分页模块
 @Author: 贤心
 @Site：http://www.layui.com/
 @License：MIT
 
 */

function laypage(options){
    return new Page(options).render()
}

var index = 0, Page = function(options){
    var that = this;
    that.query = /\/page\/\d+?([\s\S]*)$/;
    that.config = options || {};
};

laypage.v = '1.x';

//分页视图
Page.prototype.view = function(){
    var that = this, conf = that.config, view = [], dict = {};
    conf.pages = conf.pages|0;
    conf.curr = (conf.curr|0) || 1;
    conf.groups = 'groups' in conf ? (conf.groups|0) : 3;
    conf.first = 'first' in conf ? conf.first : 1;
    conf.last = 'last' in conf ? conf.last : conf.pages;
    conf.prev = 'prev' in conf ? conf.prev : '\u4e0a\u4e00\u9875';
    conf.next = 'next' in conf ? conf.next : '\u4e0b\u4e00\u9875';
    conf.className = conf.className || 'laypage';
    
    conf.url = conf.url||'';
    var str='';
    if (conf.url.indexOf("?") != -1) {   
        var str = conf.url.substr(conf.url.indexOf("?"));
        console.log(str);
        if(str==undefined)
        {
            str="";
        }
    }
    console.log(str);
    
    if(!that.query.test(conf.url)){
        conf.url = conf.url.replace(/\/*$/, '/page/');
    }
    conf.url = conf.url.replace(that.query, '/page/');
    
    console.log(conf.url);
    
    if(conf.groups > conf.pages){
        conf.groups = conf.pages;
    }
    
    //计算当前组
    dict.index = Math.ceil((conf.curr + ((conf.groups > 1 && conf.groups !== conf.pages) ? 1 : 0))/(conf.groups === 0 ? 1 : conf.groups));
    
    //当前页非首页，则输出上一页
    if(conf.curr > 1 && conf.prev){
        view.push('<a href="'+ conf.url + (conf.curr - 1) +'/'+str+'" class="'+ conf.className +'-prev">'+ conf.prev +'</a>');
    }
    
    //当前组非首组，则输出首页
    if(dict.index > 1 && conf.first && conf.groups !== 0){
        view.push('<a href="'+ conf.url +'1/'+str+'" class="'+ conf.className +'-first" title="\u9996\u9875">'+ conf.first +'</a><span>\u2026</span>');
    }
    
    //输出当前页组
    dict.poor = Math.floor((conf.groups-1)/2);
    dict.start = dict.index > 1 ? conf.curr - dict.poor : 1;
    dict.end = dict.index > 1 ? (function(){
        var max = conf.curr + (conf.groups - dict.poor - 1);
        return max > conf.pages ? conf.pages : max;
    }()) : conf.groups;
    if(dict.end - dict.start < conf.groups - 1){ //最后一组状态
        dict.start = dict.end - conf.groups + 1;
    }
    for(; dict.start <= dict.end; dict.start++){
        if(dict.start === conf.curr){
            view.push('<span class="'+ conf.className +'-curr">'+ dict.start +'</span>');
        } else {
            view.push('<a href="'+ conf.url + dict.start +'/">'+ dict.start +'</a>');
        }
    }
    
    //总页数大于连续分页数，且当前组最大页小于总页，输出尾页
    if(conf.pages > conf.groups && dict.end < conf.pages && conf.last && conf.groups !== 0){
         view.push('<span>\u2026</span><a href="'+ conf.url + conf.pages +'/'+str+'" class="'+ conf.className +'-last" title="\u5c3e\u9875">'+ conf.last +'</a>');
    }
    
    //当前页不为尾页时，输出下一页
    dict.flow = !conf.prev && conf.groups === 0;
    if(conf.curr !== conf.pages && conf.next || dict.flow){
        view.push((function(){
            return (dict.flow && conf.curr === conf.pages) 
            ? '<span class="'+ conf.className +'-nomore" title="\u5df2\u6ca1\u6709\u66f4\u591a">'+ conf.next +'</span>'
            : '<a href="'+ conf.url + (conf.curr + 1) +'/'+str+'" class="'+ conf.className +'-next">'+ conf.next +'</a>';
        }()));
    }
    
    if(conf.pages <= 1){
        return '';
    }
    
    return '<div name="laypage'+ laypage.v +'" class="'+ conf.className +'-main">'+ view.join('') + function(){
        return conf.skip 
        ? '<span class="'+ conf.className +'-total"><label>\u5230\u7b2c</label><input type="number" min="1" onkeyup="this.value=this.value.replace(/\\D/, \'\');" class="'+ conf.className +'-skip"><label>\u9875</label>'
        + '<button type="button" class="'+ conf.className +'-btn">\u786e\u5b9a</button></span>' 
        : '';
    }() +'</div>';
};

//渲染
Page.prototype.render = function(){
    return this.view();
}

module.exports = laypage;